package cloud.maque.blog.impl;


import cloud.maque.blog.entity.MqUser;
import cloud.maque.blog.mapper.MqUserMapper;
import cloud.maque.blog.service.MqUserService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;

/**
 * 用户表;(mq_user)表服务实现类
 *
 * @author : maque.cloud
 * @date : 2025-2-6
 */
@Service
public class MqUserServiceImpl extends ServiceImpl<MqUserMapper, MqUser> implements MqUserService {

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    public MqUser queryById(String userId) {
        return this.getById(userId);
    }

    /**
     * 分页查询
     *
     * @param mqUser  筛选条件
     * @param current 当前页码
     * @param size    每页大小
     * @return
     */
    public IPage<MqUser> paginQuery(MqUser mqUser, long current, long size) {
        //1. 构建动态查询条件
        LambdaQueryWrapper<MqUser> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(mqUser.getTenantId())) {
            queryWrapper.eq(MqUser::getTenantId, mqUser.getTenantId());
        }
        if (StrUtil.isNotBlank(mqUser.getCreatedBy())) {
            queryWrapper.eq(MqUser::getCreatedBy, mqUser.getCreatedBy());
        }
        if (StrUtil.isNotBlank(mqUser.getUpdatedBy())) {
            queryWrapper.eq(MqUser::getUpdatedBy, mqUser.getUpdatedBy());
        }
        if (StrUtil.isNotBlank(mqUser.getUserName())) {
            queryWrapper.eq(MqUser::getUserName, mqUser.getUserName());
        }
        if (StrUtil.isNotBlank(mqUser.getNickName())) {
            queryWrapper.eq(MqUser::getNickName, mqUser.getNickName());
        }
        if (StrUtil.isNotBlank(mqUser.getPassWord())) {
            queryWrapper.eq(MqUser::getPassWord, mqUser.getPassWord());
        }
        if (StrUtil.isNotBlank(mqUser.getEmail())) {
            queryWrapper.eq(MqUser::getEmail, mqUser.getEmail());
        }
        if (StrUtil.isNotBlank(mqUser.getPhone())) {
            queryWrapper.eq(MqUser::getPhone, mqUser.getPhone());
        }
        if (StrUtil.isNotBlank(mqUser.getAvatar())) {
            queryWrapper.eq(MqUser::getAvatar, mqUser.getAvatar());
        }
        if (StrUtil.isNotBlank(mqUser.getLastLoginIp())) {
            queryWrapper.eq(MqUser::getLastLoginIp, mqUser.getLastLoginIp());
        }
        if (StrUtil.isNotBlank(mqUser.getRealName())) {
            queryWrapper.eq(MqUser::getRealName, mqUser.getRealName());
        }
        if (StrUtil.isNotBlank(mqUser.getRole())) {
            queryWrapper.eq(MqUser::getRole, mqUser.getRole());
        }
        //2. 执行分页查询
        Page<MqUser> pagin = new Page<>(current, size, true);
        IPage<MqUser> selectResult = this.baseMapper.selectByPage(pagin, queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return this.page(pagin, queryWrapper);
    }

    /**
     * 保存数据
     *
     * @param mqUser 实例对象
     * @return 实例对象
     */
    public MqUser Save(MqUser mqUser) {

        if (StrUtil.isEmptyIfStr(mqUser.getUserId())) {
            this.baseMapper.insert(mqUser);
            return mqUser;
        }
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<MqUser> chainWrapper = new LambdaUpdateChainWrapper<MqUser>(this.baseMapper);
        if (StrUtil.isNotBlank(mqUser.getTenantId())) {
            chainWrapper.eq(MqUser::getTenantId, mqUser.getTenantId());
        }
        if (StrUtil.isNotBlank(mqUser.getCreatedBy())) {
            chainWrapper.eq(MqUser::getCreatedBy, mqUser.getCreatedBy());
        }
        if (StrUtil.isNotBlank(mqUser.getUpdatedBy())) {
            chainWrapper.eq(MqUser::getUpdatedBy, mqUser.getUpdatedBy());
        }
        if (StrUtil.isNotBlank(mqUser.getUserName())) {
            chainWrapper.eq(MqUser::getUserName, mqUser.getUserName());
        }
        if (StrUtil.isNotBlank(mqUser.getNickName())) {
            chainWrapper.eq(MqUser::getNickName, mqUser.getNickName());
        }
        if (StrUtil.isNotBlank(mqUser.getPassWord())) {
            chainWrapper.eq(MqUser::getPassWord, mqUser.getPassWord());
        }
        if (StrUtil.isNotBlank(mqUser.getEmail())) {
            chainWrapper.eq(MqUser::getEmail, mqUser.getEmail());
        }
        if (StrUtil.isNotBlank(mqUser.getPhone())) {
            chainWrapper.eq(MqUser::getPhone, mqUser.getPhone());
        }
        if (StrUtil.isNotBlank(mqUser.getAvatar())) {
            chainWrapper.eq(MqUser::getAvatar, mqUser.getAvatar());
        }
        if (StrUtil.isNotBlank(mqUser.getLastLoginIp())) {
            chainWrapper.eq(MqUser::getLastLoginIp, mqUser.getLastLoginIp());
        }
        if (StrUtil.isNotBlank(mqUser.getRealName())) {
            chainWrapper.eq(MqUser::getRealName, mqUser.getRealName());
        }
        if (StrUtil.isNotBlank(mqUser.getRole())) {
            chainWrapper.eq(MqUser::getRole, mqUser.getRole());
        }
        //2. 设置主键，并更新
        chainWrapper.set(MqUser::getUserId, mqUser.getUserId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if (ret) {
            return queryById(mqUser.getUserId());
        } else {
            return mqUser;
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    public boolean deleteById(String userId) {
        int total = this.baseMapper.deleteById(userId);
        return total > 0;
    }
}